package bb.lanxing.lib.devices.sprint.ymodem;

import bb.lanxing.lib.devices.utils.ByteUtils;
import bb.lanxing.lib.devices.utils.CRC;

public class YModems {
    public static final byte ACK = 6;
    public static final byte CAN = 24;
    private static final int CRC_LEN = 2;
    private static final int DATA_OFFSETS = 3;
    public static final int DATA_SIZE = 128;
    public static final int DATA_SIZE_BIG = 1024;
    public static final byte EOT = 4;
    public static final byte NAK = 21;
    public static final int PACKAGE_SIZE = 133;
    public static final int PACKAGE_SIZE_BIG = 1029;
    public static final byte SOH = 1;
    public static final byte STX = 2;
    public static final byte ST_C = 67;
    public static byte seqNo;

    public static boolean isYModemCmd(byte b) {
        return b == 1 || b == 2 || b == 4 || b == 6 || b == 21 || b == 67;
    }

    public static synchronized byte createSeqnNo() {
        byte b;
        synchronized (YModems.class) {
            byte b2 = seqNo;
            seqNo = (byte) (b2 + 1);
            b = (byte) (b2 % 255);
            seqNo = b;
        }
        return b;
    }

    public static YModem createYModem(byte[] bArr, int i, int i2) {
        YModem yModem = new YModem();
        yModem.signal = bArr[i];
        yModem.seqNo = bArr[i + 1];
        yModem.rseqNo = bArr[i + 2];
        int i3 = (i2 - 3) - 2;
        yModem.data = new byte[i3];
        int i4 = i + 3;
        System.arraycopy(bArr, i4, yModem.data, 0, i3);
        yModem.crc = ByteUtils.toShortBE(bArr, i4 + i3);
        return yModem;
    }

    public static YModem createYModem(byte[] bArr) {
        return createYModem(bArr, 0, bArr.length);
    }

    public static YModem createYModem(byte b, byte b2, byte[] bArr) {
        int length = bArr != null ? bArr.length : 0;
        YModem yModem = new YModem();
        yModem.signal = b;
        yModem.data = bArr;
        yModem.seqNo = b2;
        yModem.rseqNo = (byte) ((~b2) & 255);
        yModem.crc = CRC.getCrc16(bArr, 0, length);
        return yModem;
    }

    public static byte[] createYModemRaw(byte b, byte b2, byte[] bArr) {
        int length = bArr != null ? bArr.length : 0;
        int i = length + 3;
        byte[] bArr2 = new byte[i + 2];
        bArr2[0] = b;
        bArr2[1] = b2;
        bArr2[2] = (byte) ((~b2) & 255);
        if (length > 0) {
            System.arraycopy(bArr, 0, bArr2, 3, length);
        }
        ByteUtils.shortToBytesBe(CRC.getCrc16(bArr, 0, length), bArr2, i);
        return bArr2;
    }

    public static byte[] truncate(byte[] bArr, int i) {
        if (bArr == null) {
            return null;
        }
        int length = bArr.length;
        while (true) {
            if (i >= bArr.length) {
                break;
            } else if (ByteUtils.assertNull(bArr, i, bArr.length - i)) {
                length = i;
                break;
            } else {
                i++;
            }
        }
        if (length == bArr.length) {
            return bArr;
        }
        byte[] bArr2 = new byte[length];
        System.arraycopy(bArr, 0, bArr2, 0, length);
        return bArr2;
    }

    public static byte[] toBytes(YModem yModem) {
        int length = yModem.data != null ? yModem.data.length : 0;
        int i = length + 3;
        byte[] bArr = new byte[i + 2];
        bArr[0] = yModem.signal;
        bArr[1] = yModem.seqNo;
        bArr[2] = yModem.rseqNo;
        if (length > 0) {
            System.arraycopy(yModem.data, 0, bArr, 3, length);
        }
        ByteUtils.shortToBytesBe(yModem.crc, bArr, i);
        return bArr;
    }

    public static boolean isYModemCmd(byte[] bArr) {
        return ByteUtils.assertNull(bArr, 1, bArr.length - 1) && isYModemCmd(bArr[0]);
    }

    public static boolean checkCRC(byte[] bArr) {
        int length = (bArr.length - 3) - 2;
        return CRC.getCrc16(bArr, 3, length) == ByteUtils.toShortBE(bArr, length + 3);
    }

    public static boolean checkCRC(YModem yModem) {
        byte[] bArr = yModem.data;
        return CRC.getCrc16(bArr, 0, bArr.length) == yModem.crc;
    }

    public static String toString(YModem yModem) {
        return "Signal: " + ((int) yModem.signal) + "\nSeqNo: " + ((int) yModem.seqNo) + "\nRseqNo: " + ((int) yModem.rseqNo) + "\nData: " + ByteUtils.printlnByteArrayToUnsignHexString(yModem.data) + "\nCRC: " + ((int) yModem.crc) + "\n";
    }

    public static String toString(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        int length = (bArr.length - 3) - 2;
        sb.append("Signal: ");
        sb.append((int) bArr[0]);
        sb.append("\n");
        sb.append("SeqNo: ");
        sb.append((int) bArr[1]);
        sb.append("\n");
        sb.append("RseqNo: ");
        sb.append((int) bArr[2]);
        sb.append("\n");
        sb.append("Data: ");
        sb.append(ByteUtils.printlnByteArrayToUnsignHexString(bArr, 3, length));
        sb.append("\n");
        sb.append("CRC: ");
        sb.append((int) ByteUtils.toShortBE(bArr, length + 3));
        sb.append("\n");
        return sb.toString();
    }
}
